﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;

namespace WebApp
{
    public partial class Home : System.Web.UI.Page
    {
        private string strconn;
        SqlCommand cmd;
        SqlConnection conn;
        DataSet ds;
        SqlDataAdapter da;
        protected void Page_Load(object sender, EventArgs e)
        {
            txtpass.Attributes["value"] = txtpass.Text;
            strconn = "Data Source = (local); Database =ProjectSem3; User id = sa; Password = 30041975";
            conn = new SqlConnection(strconn);
            ds = new DataSet();
            
        
        }
        protected void load()
        {
            try
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                cmd = new SqlCommand("BindCus", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                da = new SqlDataAdapter(cmd);
                da.Fill(ds);

            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }
        protected void btnlogin_Click(object sender, EventArgs e)
        {
            Application["userid"] = txtuser.Text;
        if (Application["guestid"] != null)
        {
            Application.Lock();
            Application["guestid"] = Convert.ToInt32(Application["guestid"]) + 1;
            Application.UnLock();
        }
        else
        {
            Application["guestid"] = 1;
        }
        Response.Cookies["login"]["userid"] = txtuser.Text;
        Response.Cookies["login"]["pass"] = txtpass.Text;
        Response.Cookies["login"]["guestid"] = Application["guestid"].ToString();
        Response.Cookies["login"].Expires = DateTime.MaxValue;
        try
        {
            
           

            if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd = new SqlCommand("userlogin", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = txtuser.Text;
            cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = txtpass.Text;
            //lbl1.Text = "success";

            object i = cmd.ExecuteScalar();
            if (i != null)
            {
                if (Request.Cookies["login"]["userid"] == txtuser.Text && Request.Cookies["login"]["pass"] == txtpass.Text)
                {
                    Application["userid"] = txtuser.Text;
                    Application["guestid"] = Request.Cookies["login"]["guestno"];
                    lbluser1.Visible = false;
                    lblpass.Visible = false;
                    txtuser.Visible = false;
                    txtpass.Visible = false;
                    btnlogin.Visible = false;
                    lblwelcome.Visible = true;
                    lbstatus.Visible = true;
                    linkbut.Visible = true;
                    lbstatus.Text = Application["userid"].ToString();
                }
                
              
            }
            else
            {
                lbstatus.Visible = true;
                lbstatus.Text = "Login false. Please check your username and password";//SAI
            }
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
        }
    }
